
import zxingFun from './zxing';
const ZXing = zxingFun();
export default class Analyzer {
    constructor(video) {
        this.video = video;

        this.imageBuffer = null;
        this.sensorLeft = null;
        this.sensorTop = null;
        this.sensorWidth = null;
        this.sensorHeight = null;

        this.canvas = document.createElement('canvas');
        this.canvas.style.display = 'none';
        this.canvasContext = null;

        this.decodeCallback = ZXing.Runtime.addFunction(function (
            ptr,
            len,
            resultIndex,
            resultCount
        ) {
            let result = new Uint8Array(ZXing.HEAPU8.buffer, ptr, len);
            let str = String.fromCharCode.apply(null, result);
            if (resultIndex === 0) {
                window.zxDecodeResult = '';
            }
            window.zxDecodeResult += str;
        });
    }

    analyze() {
        if (!this.video.videoWidth) {
            return null;
        }

        if (!this.imageBuffer) {
            let videoWidth = this.video.videoWidth;
            let videoHeight = this.video.videoHeight;

            this.sensorWidth = videoWidth;
            this.sensorHeight = videoHeight;
            this.sensorLeft = Math.floor(videoWidth / 2 - this.sensorWidth / 2);
            this.sensorTop = Math.floor(videoHeight / 2 - this.sensorHeight / 2);

            this.canvas.width = this.sensorWidth;
            this.canvas.height = this.sensorHeight;

            this.canvasContext = this.canvas.getContext('2d');
            this.imageBuffer = ZXing._resize(this.sensorWidth, this.sensorHeight);
            return null;
        }

        this.canvasContext.drawImage(
            this.video,
            this.sensorLeft,
            this.sensorTop,
            this.sensorWidth,
            this.sensorHeight
        );

        let data = this.canvasContext.getImageData(0, 0, this.sensorWidth, this.sensorHeight).data;
        for (let i = 0, j = 0; i < data.length; i += 4, j++) {
            let [r, g, b] = [data[i], data[i + 1], data[i + 2]];
            ZXing.HEAPU8[this.imageBuffer + j] = Math.trunc((r + g + b) / 3);
        }

        let err = ZXing._decode_qr(this.decodeCallback);
        if (err) {
            return null;
        }

        let result = window.zxDecodeResult;
        if (result != null) {
            return { result: result, canvas: this.canvas };
        }

        return null;
    }
}
